import DataColumnString from './dataColumnString.js'
import DataColumnLong from './dataColumnLong.js'
import DataColumnDecimal from './dataColumnDecimal.js'
import DataColumnDate from './dataColumnDate.js'
import DataColumnTime from './dataColumnTime.js'
import DataColumnDateTime from './dataColumnDateTime.js'
import DataColumnBoolean from './dataColumnBoolean.js'
import DataColumnEnum from './dataColumnEnum.js'
import GeneralDataRow from './generalDataRow.js'
// import { v4 as uuidv4 } from 'uuid'
export default class GeneralDataTable {
	constructor(name){
		if (name) {
			this.id = this.guid();
			this.name = name
			this.label = name
		} else {
			alert ("数据表名称不允许为空")
		}
	}
	
	dataColumns = []
	dataRows = []
	
	createStringDataColumn(columnName, label, stringSize) {
		if (Object.keys(this.getDataColumn(columnName)).length) {
			// alert("数据区块"+columnName+"已存在数据列"+columnName+"，不允许重复添加")
		} else {
			let dataColumn = new DataColumnString(columnName, stringSize);
			dataColumn.setLabel(label);
			this.dataColumns.push(dataColumn);
		}
	}
	
	
	createLongDataColumn(columnName, label) {
		if (Object.keys(this.getDataColumn(columnName)).length) {
			alert("数据区块"+columnName+"已存在数据列"+columnName+"，不允许重复添加")
		} else {
			let dataColumn = new DataColumnLong(columnName);
			dataColumn.setLabel(label);
			this.dataColumns.push(dataColumn);
		}
	}
	
	createDecimalDataColumn(columnName, label, numOfDecimals) {
		if (Object.keys(this.getDataColumn(columnName)).length) {
			alert("数据区块"+columnName+"已存在数据列"+columnName+"，不允许重复添加")
		} else {
			let dataColumn = new DataColumnDecimal(columnName, numOfDecimals);
			dataColumn.setLabel(label);
			this.dataColumns.push(dataColumn);
		}
	}
	
	createDateDataColumn(columnName, label) {
		if (Object.keys(this.getDataColumn(columnName)).length) {
			alert("数据区块"+columnName+"已存在数据列"+columnName+"，不允许重复添加")
		} else {
			let dataColumn = new DataColumnDate(columnName);
			dataColumn.setLabel(label);
			this.dataColumns.push(dataColumn);
		}
	}
	
	createTimeDataColumn(columnName, label) {
		if (Object.keys(this.getDataColumn(columnName)).length) {
			alert("数据区块"+columnName+"已存在数据列"+columnName+"，不允许重复添加")
		} else {
			let dataColumn = new DataColumnTime(columnName);
			dataColumn.setLabel(label);
			this.dataColumns.push(dataColumn);
		}
	}
	
	createDateTimeDataColumn(columnName, label) {
		if (Object.keys(this.getDataColumn(columnName)).length) {
			alert("数据区块"+columnName+"已存在数据列"+columnName+"，不允许重复添加")
		} else {
			let dataColumn = new DataColumnDateTime(columnName);
			dataColumn.setLabel(label);
			this.dataColumns.push(dataColumn);
		}
	}
	
	createBooleanDataColumn(columnName, label) {
		if (Object.keys(this.getDataColumn(columnName)).length) {
			alert("数据区块"+columnName+"已存在数据列"+columnName+"，不允许重复添加")
		} else {
			let dataColumn = new DataColumnBoolean(columnName);
			dataColumn.setLabel(label);
			this.dataColumns.push(dataColumn);
		}
	}
	
	createEnumDataColumn(columnName, label, enumItems) {
		if (Object.keys(this.getDataColumn(columnName)).length) {
			alert("数据区块"+columnName+"已存在数据列"+columnName+"，不允许重复添加")
		} else {
			let dataColumn = new DataColumnEnum(columnName, enumItems);
			dataColumn.setLabel(label);
			this.dataColumns.push(dataColumn);
		}
	}

	getDataColumn(columnName) {
		const columnItem = this.dataColumns.filter(item => {
			return item.name === columnName
		})
		return columnItem
	}

	getDataColumns() {
		return this.dataColumns;
	}
	
	removeDataColumn(dataColumnName) {
		let delIndex
		this.dataColumns.forEach((item,index) => {
			if (item.name === dataColumnName) {
				delIndex = index
			}
		})
		this.dataColumns.splice(delIndex,1)
	}
	
	getDataRows(){
		return this.dataRows;
	}
	
	createDataRow(rowData = null) {
		if (rowData === null) {
			const dataRow = new GeneralDataRow(this);
			this.dataRows.push(dataRow);
			return dataRow;
		} else {
			const dataRow = createDataRow();
			if (Object.keys(rowData).length) {
				for (let key in dataRow) {
					dataRow.setCellValue(key, dataRow[key])
				}
			}
			return dataRow;
		}
	}
	guid() {
		return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
			var r = Math.random() * 16 | 0,
				v = c == 'x' ? r : (r & 0x3 | 0x8);
			return v.toString(16);
		});
	}
}